//
//  YGWelcomViewController.swift
//  sinaWeiBoGui
//
//  Created by liyonggui on 16/5/14.
//  Copyright © 2016年 liyonggui. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

class YGWelcomViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加子控制件
        prepareUI()
        
        self.setInfo()
        
        // 加载用户信息
        YGUserAccountViewModel.sharedInstance.losdUserInfo { () -> () in
            self.setInfo()
        }
    }
    
    private func setInfo() {
        if let avatar_large = YGUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            // 设置头像
            let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 看到界面开始动画
        moveAnimation()
    }
    
    private func moveAnimation() {
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            // 根据约束重新设置frame
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                YGPrint(items: "动画完成")
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.msgLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        let myAppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        myAppDelegate.switchController(YGMainTabBarViewController())
                })
        }
    }
    
    
    /// 添加子控制件
    private func prepareUI() {
        
        view.addSubview(bkgView)
        view.addSubview(iconView)
        view.addSubview(msgLabel)
        
        // 添加约束
        // 背景
        bkgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
        
        // 消息框
        msgLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    // MARK:- 懒加载
    /// 背景
    private lazy var bkgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像  avatar_default_big
    private lazy var iconView: UIImageView =  {
       let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
    
        return iconView
    }()
    
    /// 消息框
    private lazy var msgLabel: UILabel = {
       let label = UILabel()
        
        // 设置label属性
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(15)
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()

}


























